package org.ponly.cta4j.factory.gimpy;

import java.security.SecureRandom;
import java.util.Locale;
import java.util.Random;

/**
 * 随机文本生成器.
 *
 * @author vacoor
 * @since 1.0
 */
public class RandomTextGenerator implements TextGenerator {
    /**
     * random for chars.
     */
    protected final Random random = new SecureRandom();

    /**
     * random chars.
     */
    private char[] possiblesChars;

    /**
     * 创建一个默认的随机文本生成器, 使用默认文本种子 a-z0-9.
     */
    public RandomTextGenerator() {
        this("abcdefghijklmnopqrstuvwxyz0123456789");
    }

    /**
     * 使用给定的字符种子创建一个随机文本生成器.
     *
     * @param possiblesChars 随机种子
     */
    public RandomTextGenerator(final String possiblesChars) {
        this(possiblesChars.toCharArray());
    }

    /**
     * 使用给定的字符种子创建一个随机文本生成器.
     *
     * @param possiblesChars 随机种子
     */
    public RandomTextGenerator(final char[] possiblesChars) {
        this.possiblesChars = possiblesChars.clone();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String nextText(final int length) {
        final StringBuilder buff = new StringBuilder();
        for (int i = 0; i < length; i++) {
            buff.append(possiblesChars[random.nextInt(possiblesChars.length)]);
        }
        return buff.toString();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String nextText(final int length, final Locale locale) {
        return nextText(length);
    }
}
